package com.com.effectivejava.generics.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by pankaj on 7/20/14.
 */
public class Union<T> {

    Collection<T> backedCollection1 = new ArrayList<T>();
    Collection<T> backedCollection2 = new ArrayList<T>();

    // Producer extends:
    // who is producer? backendCollection1 or collection1
    // it is collection1 as it is producing elements for backendCollection1 to consume.
    // Am i sure?
    public void add(Collection<? extends T> collection1, Collection<? extends T> collection2){
        for(T t : collection1){
           backedCollection1.add(t);
        }

        for(T t: collection2){
           backedCollection2.add(t);
        }
    }

    //Consumer super:
    //who is consumer? backendCollection1 or result
    //It result that is consuming the elements produced by backendCollection1
    //Am i sure?
    public void union(Collection<? super T> result){
        Set<T> set1 = new HashSet<>(backedCollection1);
        Set<T> set2 = new HashSet<>(backedCollection2);

        Set<T> temp = new HashSet<>(set1);
        temp.addAll(set2);

        result.addAll(temp);
    }
}
